package com.neu.flashmall.order.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.flashmall.order.entity.OmsOrder;
import com.neu.flashmall.order.entity.OmsOrderItem;
import com.neu.flashmall.order.mapper.OmsOrderItemMapper;
import com.neu.flashmall.order.mapper.OmsOrderMapper;
import com.neu.flashmall.order.service.OmsOrderService;
import com.neu.flashmall.order.vo.OmsOrderVo;
import com.neu.flashmall.order.vo.OrderVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OmsOrderServiceImpl implements OmsOrderService {
    @Resource
    private OmsOrderMapper omsOrderMapper;

    @Resource
    private OmsOrderItemMapper omsOrderItemMapper;

    @Override
    public PageInfo<OmsOrderVo> selectAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<OmsOrderVo> omsOrders = omsOrderMapper.selectAll();

        PageInfo<OmsOrderVo> pageInfo = new PageInfo<>(omsOrders);
        return pageInfo;
    }

    @Override
    public String changeConfirm(Long orderId) {
        OmsOrder omsOrder = omsOrderMapper.selectByPrimaryKey(orderId);
        if(omsOrder.getConfirmStatus() == 0){
            omsOrder.setConfirmStatus(1);
            omsOrder.setStatus(3);
            if(omsOrderMapper.updateByPrimaryKey(omsOrder) > 0){
                return "收货成功";
            }
            return "收货失败";
        }
        return "该订单已收货";
    }

    @Override
    public String changeDelete(Long orderId) {
        OmsOrder omsOrder = omsOrderMapper.selectByPrimaryKey(orderId);
        if(omsOrder.getDeleteStatus() == 0){
            omsOrder.setDeleteStatus(1);
            omsOrderMapper.updateByPrimaryKey(omsOrder);
            return "修改删除状态成功";
        }else if(omsOrder.getDeleteStatus() == 1){
            omsOrder.setDeleteStatus(0);
            omsOrderMapper.updateByPrimaryKey(omsOrder);
            return "修改删除状态成功";
        }
        return "修改删除状态失败";
    }

    @Override
    public PageInfo<OmsOrderVo> getOrderByStatus(Integer pageNum,Integer pageSize,String statusName) {
        int status;
        if(statusName.equals("unpay")){
            //获取全部未支付订单
            status = 0;
        }else if(statusName.equals("deliver")){
            //获取全部待发货订单
            status = 1;
        }else if(statusName.equals("receive")){
            //获取全部已发货订单
            status = 2;
        }else if(statusName.equals("complete")){
            //获取全部已完成订单
            status = 3;
        }else if(statusName.equals("cancel")){
            //获取全部已关闭订单(未评论
            status = 4;
        }else if(statusName.equals("unable")){
            //获取全部无效订单
            status = 5;
        }else{
            status = -1;
        }
        PageHelper.startPage(pageNum,pageSize);
        List<OmsOrderVo> list;
        if(status == -1){
            list = omsOrderMapper.selectAll();
        }else{
            list = omsOrderMapper.selectOrderByStatus(status);
        }

        PageInfo<OmsOrderVo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public String addOrder(OrderVo orderVo) {
        OmsOrder order = orderVo.getOrder();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String orderCode = "O" + simpleDateFormat.format(new Date());
        order.setOrderCode(orderCode);
        order.setCreateTime(new Date());
        int result = omsOrderMapper.insertSelective(order);
        if(result == 1){
            List<OmsOrderItem> list = orderVo.getOrderItemList();
            for(OmsOrderItem omsOrderItem : list){
                omsOrderItem.setOrderCode(orderCode);
                omsOrderItem.setOrderId(order.getId());
                int result2 = omsOrderItemMapper.insertSelective(omsOrderItem);
                if(result2 != 1) return "数据库异常";
            }
            return "添加订单成功";
        }
        return "数据库异常";
    }

    @Override
    public OmsOrderVo getOrderById(Long orderId) {
        OmsOrderVo order = omsOrderMapper.getOrderVoById(orderId);
        order.setCountdown(60L);
        return order;
    }

    @Override
    public OmsOrder selectByPrimaryKey(Long orderId) {
        return omsOrderMapper.selectByPrimaryKey(orderId);
    }

    @Override
    public OmsOrder selectByOrderCode(String orderCode) {
        return omsOrderMapper.getOrderByOrderCode(orderCode);
    }

    @Override
    public String changeStatus(String orderCode) {
        OmsOrder order = omsOrderMapper.getOrderByOrderCode(orderCode);
        if(order.getStatus() == 0){
            //待付款
            order.setStatus(1);
            //变为待发货状态
            if(omsOrderMapper.updateByPrimaryKey(order) > 0){
                return "付款成功";
            }
            return "付款失败";
        }else if(order.getStatus() == 1){
            //待发货
            order.setStatus(2);
            //变为待收货状态
            if(omsOrderMapper.updateByPrimaryKey(order) > 0){
                return "发货成功";
            }
            return "发货失败";
        }
        return "订单状态更改失败";
    }

    @Override
    public List<OrderVo> getOrderVo(Long userId,Integer pageSize,Integer pageNum,Integer status) {
        pageNum = (pageNum - 1) * pageSize;
        List<OrderVo> result = new ArrayList<>();
        List<OmsOrder> orderList;
        if(status == -1){
            orderList = omsOrderMapper.selectByUserId(userId,pageSize,pageNum);
        }else{
            orderList = omsOrderMapper.selectAllByUserAndStatus(userId,pageSize,pageNum,status);
        }
        for(OmsOrder o : orderList){
            List<OmsOrderItem> orderItemList = omsOrderItemMapper.selectAll(o.getId());
            OrderVo orderVo = new OrderVo();
            orderVo.setOrder(o);
            orderVo.setOrderItemList(orderItemList);
            result.add(orderVo);
        }
        return result;
    }

    @Override
    public int changeStatus(Long orderId, Integer status) {
        return omsOrderMapper.changeStatus(orderId,status);
    }

    @Override
    public int falseDelete(Long id) {
        return omsOrderMapper.deleteFalse(id);
    }


    public OrderVo getOrderVo(Long orderId){
        OmsOrder omsOrder = omsOrderMapper.selectByPrimaryKey(orderId);
        List<OmsOrderItem> orderItemList = omsOrderItemMapper.selectAll(omsOrder.getId());
        OrderVo orderVo = new OrderVo();
        orderVo.setOrder(omsOrder);
        orderVo.setOrderItemList(orderItemList);
        return orderVo;
    }

    @Override
    public int update(OmsOrder order) {
        return omsOrderMapper.updateByPrimaryKeySelective(order);
    }
}
